﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmLinhVuc : DevExpress.XtraEditors.XtraForm
    {
        #region Khai bao biến

        DataTable dt = new DataTable();
        LinhVuc_BLL lv_bll = new LinhVuc_BLL();
        TextMessageBox box = new TextMessageBox();
        public string maLinhVuc="";
        private bool isOK = false;
        #endregion


        #region LoadForm

        public frmLinhVuc()
        {
            InitializeComponent();
        }

        private void frmLinhVuc_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region cac chuc nang

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOK = false;
            UpdateData();
        }
        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ClearText();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion

        #region cac su kien

        private void frmLinhVuc_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }

        #endregion

        #region ham viet them
        private void UpdateData()
        {
            if (checkValid())
            {
                LinhVuc_Public lv = new LinhVuc_Public();
                if (maLinhVuc == "")
                {
                    if (checkTenInsert())
                    {
                        lv.MaLinhVuc = box.AutoID("LV", 2, lv_bll.LayDuLieu());
                        lv.TenLinhVuc = box.toUpper(txtTen.Text);
                        lv.GhiChu = txtGhiChu.Text;

                        lv_bll.Insert(lv, lv, lv);
                        CloseForm();
                    }
                }
                else
                {
                    lv.MaLinhVuc = txtMaLinhVuc.Text.Trim();
                    lv.TenLinhVuc = box.toUpper(txtTen.Text);
                    lv.GhiChu = txtGhiChu.Text;

                    lv_bll.Update(lv, lv, lv);
                    CloseForm();
                }
            }
        }
        private void CloseForm()
        {
            if (isOK == true)
                DialogResult = DialogResult.OK;
            else
                ClearText();
        }
        private void LoadData()
        {
            if (maLinhVuc != "")
            {
                dt = lv_bll.TaoBang(box.sqlCheckExist("DM_LinhVuc","MaLinhVuc",maLinhVuc));
                DataRow dr = dt.Rows[0];
                txtMaLinhVuc.Text = maLinhVuc;
                txtTen.Text=dr["TenLinhVuc"].ToString();
                txtGhiChu.Text = dr["GhiChu"].ToString();
            }
        }
        private void ClearText()
        {
            
            txtMaLinhVuc.Text = "Mã tự sinh";
            txtTen.Text = "";
            txtGhiChu.Text = "";
            txtTen.Focus();
            maLinhVuc = "";
        }
        private bool checkValid()
        {
            if (txtTen.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên lĩnh vực.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTen.Focus();
                return false;
            }
            return true;
        }

        private bool checkTenInsert()
        {
            dt = lv_bll.TaoBang(box.sqlCheckExist("DM_LinhVuc","TenLinhVuc",txtTen.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTen.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTen.Focus();
                return false;
            }
            return true;
        }

        #endregion


    }
}